<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Subscriber extends CI_Controller {
    
   	public function __construct()
    {
        parent::__construct();
        $this->load->model('MSubscriber');
    }
    
	public function index()
	{
	    //$data['encoding'] = 'utf-8';
//        $data['feed_name'] = 'www.chamroeun.com';
//        $data['feed_url'] = 'http://www.chamroeun.com';
//        $data['page_description'] = 'Chamroeun Microfinance Limited - hot articles';
//        $data['page_language'] = 'en-en';
//        $data['creator_email'] = 'bunthorn@chamroeun.com';  
//        $data['articles'] = $this->MPage->getLatestArticles(5);
//        $this->load->vars($data);
//        header("Content-Type: application/rss+xml");
//        $this->load->view('site/rssfeed');	   
	}
	
    public function add()
    {
       if($this->input->post('subscriber'))
       {
            $this->MSubscriber->addSubscriber();
            $this->session->set_flashdata('message','Successfully subscribed!');
       }
       redirect($this->input->post('redirect'), 'refresh');
    }
    
    public function feedback()
    {
       if($this->input->post('name'))
       {
            $this->MSubscriber->addFeedback();
            $config = Array(
    			  'protocol' => 'smtp',
    			  'smtp_host' => 'mail.chamroeun.com',
    			  'smtp_port' => 25,//465,
    			  'smtp_user' => 'bunthorn@chamroeun.com', // change it to yours
    			  'smtp_pass' => 'sZad9tHg', // change it to yours
    			  'mailtype' => 'html',
    			  'charset' => 'utf-8'
    		);	
            //$list = $this->input->post('emails');
    		$this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            
            $this->email->set_mailtype("html");
            $name = $this->input->post('name');
            $from = $this->input->post('email');
            $subj = $this->input->post('subject');
            $msg = $this->input->post('message');
            
    		$this->email->from($from, $name);
    		//$this->email->to($list);
            //$this->email->to(array("bunthorn@chamroeun.com","chandy@chamroeun.com"));
            $this->email->to("bunthorn@chamroeun.com");
    		$this->email->subject($subj);
    		$this->email->message($msg);
    			
    		$message = "Sorry unable to send email...";	
    		if($this->email->send()){					
    			$message = "Thank you for your feedback, our team will try to review it soon.";			
    		}	
        	$this->session->set_flashdata('message',$message);
            redirect('page/feedback', 'refresh');
       }
       redirect($this->input->post('redirect'), 'refresh');
    }
    
    public function trackvisitor()
    {
        if($this->input->post('ip')){
            $this->MSubscriber->addGeoVisitor();
            echo "Status: Ok";
        }
        else
            echo "Status: Error";
    }
    
}
